/**
 * @author 雷明浩
 * @date 2025/6/26 16:24
 */
public class lowestCommonAncestor {
    // 在二叉树中找到两个节点的最近公共祖先
    // https://www.nowcoder.com/practice/e0cc33a83afe4530bcec46eba3325116?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D295
    public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
        // write code here
        if (root == null) {
            return -1;
        }
        if (root.val == o1 || root.val == o2) {
            return root.val;
        }
        int leftParent = lowestCommonAncestor(root.left, o1, o2);
        int rightParent = lowestCommonAncestor(root.right, o1, o2);
        if (leftParent != -1 && rightParent != -1) {
            return root.val;
        } else if (leftParent != -1) {
            return leftParent;
        } else if (rightParent != -1) {
            return rightParent;
        }
        return -1;
    }
}